#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>

//猜数游戏

int random_num(int x, int y) {
    srand((int) time(NULL));
    int num = rand() % (y + 1 - x) + x;
    return num;
}

void menu() {
    printf("*************************************\n");
    printf("****    1.PLAY       0.EXIT      ****\n");
    printf("*************************************\n");
}

void game() {

    int num1, num2;
    int guess;
    int number;
    int count;
    //printf("随机数为:%d\n", number);
    printf("欢迎来到猜数游戏!\n请输入范围:>");
    scanf("%d%d", &num1, &num2);
    int min = (num1 < num2) ? num1 : num2;
    int max = (num1 > num2) ? num1 : num2;
    printf("你要猜几次:>");
    scanf("%d", &count);
    number = random_num(min, max);
    for (int i = 0; i < count; i++) {
        printf("请输入你猜的数:>");
        scanf("%d", &guess);
        if (guess == number) {
            printf("猜对了!\n");
            break;
        } else
        if (guess > number) {
            printf("猜大了!\n");
        } else
        if (guess < number) {
            printf("猜小了!\n");
        }
        if ((i + 1) == count) {
            printf("正确答案:%d\n", number);
        }
    }
    printf("本局结束!\n");
}

int main() {
    int choice;

    do {
        menu();
        printf("请输入你的选择:>");
        scanf("%d", &choice);
        switch (choice) {
            case 1:
                game();
                break;
            case 0:
                printf("游戏结束!");
                break;
            default:
                printf("输入错误!");
                break;
        }
    } while (choice);
    return 0;
}